package cn.lystudio.rohmtimeopwide_background_v11.controller.webApi;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.lystudio.rohmtimeopwide_background_v11.entity.po.SystemSettingPO;
import cn.lystudio.rohmtimeopwide_background_v11.service.SystemSettingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/webapi/systemSetting")
@CrossOrigin
@Slf4j
public class SystemSettingController {
	private SystemSettingService systemSettingService;
	@Autowired
	public void setSystemSettingService(SystemSettingService systemSettingService) {
		this.systemSettingService = systemSettingService;
	}

	@GetMapping("/getDailyReportAddress")
	@SaCheckLogin
	@SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
	public SaResult getDailyReportAddress() {
		LambdaQueryWrapper<SystemSettingPO> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SystemSettingPO::getSettingKey,"dailyReportAddress")
				.last("LIMIT 1");
		return SaResult.data(systemSettingService.getOne(wrapper));
	}
	@PostMapping("/setDailyReportAddress")
	@SaCheckLogin
	@SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
	public SaResult setDailyReportAddress(@RequestBody SystemSettingPO systemSettingPO) {
		LambdaQueryWrapper<SystemSettingPO> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(SystemSettingPO::getSettingKey,"dailyReportAddress");
		boolean res = systemSettingService.update(systemSettingPO, wrapper);
		if (res) {
			return SaResult.ok();
		} else {
			return SaResult.error("设置失败");
		}
	}
}
